package com.apobates.forum.toro.tuple.strategy;

import org.apache.commons.lang3.tuple.Pair;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 左对象与右对象一对多模式的执行策略
 * @param <L> 左对象类型
 * @param <R> 右对象类型
 * @param <U> 右对象查询函数参数的类型
 */
public class AnyPairSupplyFutureStrategy<L,R,U> extends AbstractPairFutureStrategy<L,R,U> implements AnyPairSupplyStrategy<L,R,U> {
    public AnyPairSupplyFutureStrategy(int timeout, TimeUnit unit) {
        super(timeout, unit);
    }

    public AnyPairSupplyFutureStrategy() {
    }

    @Override
    public Pair<L, Collection<R>> apply(Supplier<L> leftObjFun, Function<U, Collection<R>> rightFun, Map<String, String> arguments) {
        // 左
        CompletableFuture<L> lFuture = CompletableFuture
                .supplyAsync(()->leftObjFun.get())
                .completeOnTimeout(null, getTimeout(), getUnit());
        return super.getAny(lFuture, rightFun, arguments);
    }
}
